﻿/* 
 * author: www.cnblogs.com/luminji
 * date  : 2011/12/28
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Luminji.Core.Enterprise.RBAC;
using Luminji.Exam.Application;
using Luminji.Exam.Model.Users;
using Luminji.Core.Web.Cookie;
using Luminji.ExamWeb.Models;

namespace Luminji.ExamWeb.Helpers
{

    [Authority]
    public abstract class BaseExamControl : Controller, IAuthority
    {
        //protected static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        IUser _currentUser;

        public IUser CurrentUser
        {
            get 
            {
                if (_currentUser == null)
                {
                    _currentUser = GetCurrentUser();
                }
                return _currentUser; 
            }
            set { _currentUser = value; }
        }

        private IUser GetCurrentUser()
        {
            // this.Request is available in these methods:
            // public ActionResult Sample(....)
            // because of the httpcontext
            var user = CookieHelper.GetCookieValue<LogOnModel>(this.Request, "LogOnModel");
            if (user == null)
            {
                return new ExamUser()
                {
                    CurrentUserName = "Guest",
                    CurrentUserRoles = new List<Role>()
                             {
                                 new Role(){Name = "Common", Tasks = new List<TaskAttribute>()}
                             }
                };
            }
            else
            {
                return new ExamUser()
                {
                    CurrentUserName = user.UserName,
                    UserKey = user.Password,
                };
            }
        }





    }
}